<?php

class ome_mdl_return_process extends dbeav_model{

/*
 *  售后服务已处理商品详情
 *  @param int $por_id
 */
    function product_detail($por_id){
        $product_detail = $this->dump($por_id);
        $oBranch = &$this->app->model('branch');

        $product_detail['branch_name'] = $oBranch->Get_name($product_detail['branch_id']);
        $oProduct_items = &$this->app->model('return_process_items');
        $oProblem = &$this->app->model('return_product_problem');
        $process_data = $product_detail['process_data'];
        /*仓库信息*/
      
        $product_detail['process_data'] = $process_data!='' ? unserialize($process_data):'';
        $items = $oProduct_items->getList('*',array('por_id'=>$por_id));

        foreach($items as $k=>$v){
            $items[$k]['branch_name']=$oBranch->Get_name($v['branch_id']);
            $items[$k]['is_check']=$v['is_check']=='false' ? '否':'是';
        }
 
        $product_detail['items'] = $items;
        //$problem_type=$oProblem->getList('problem_id,name',array('is_problem'=>'true','type'=>'problem_type'));
        //$UnProblemType =$oProblem->getList('problem_id,name',array('is_problem'=>'false','type'=>'problem_type'));
        //$ProblemBelong =$oProblem->getList('problem_id,name',array('is_problem'=>'true','type'=>'problem_belong'));
        //$UnProblemBelong = $oProblem->getList('problem_id,name',array('is_problem'=>'false','type'=>'problem_belong'));;
        //$problem_type = $oProblem->getList('problem_id,cat_name', array('parent_id'=>'0'), 0, -1);
        $objCat = &app::get('ome')->model('return_product_problem');
        $catList =$objCat->getCatList(0);
        $product_detail['problem_type']= $catList;
        $product_detail['UnProblemType']=$UnProblemType;
        $product_detail['ProblemBelong']= $ProblemBelong;
        $product_detail['UnProblemBelong']= $UnProblemBelong;
        $product_detail['StockType']=$oProblem->store_type();
        /*日志列表*/
        $oOperation_log = &$this->app->model('operation_log');
        $oOperation = &$this->app->model('operations');

        $product_detail['log']=$oOperation_log->read_log(array('obj_type'=>'return_product@ome','obj_id'=>$product_detail['return_id']),0,20,'log_id');
        return $product_detail;
    }

     /*
     * 当收货完成后改变主表状态
     * @status recieved verify
     */
    function changestatus($por_id,$return_id,$type,$status){
        $oProduct = &$this->app->model('return_product');
        if($type=='recieved'){
            $sqlstr = ' AND recieved=\'true\'';
        }else if($type=='verify'){
            $sqlstr = ' AND verify=\'true\'';
        }
        $sql='select count(*) AS count from  sdb_ome_return_process where return_id='.$return_id.$sqlstr;
        $pro=$this->db->selectrow($sql);
        $sql='select count(*) AS count from  sdb_ome_return_process where  return_id='.$return_id;
        $pro_need=$this->db->selectrow($sql);
        if($pro['count']==$pro_need['count']){
            $data = array(
                'return_id'=>$return_id,
                'status'=>$status
            );
            $oProduct->save($data);
        }
    }
    /*
     * 校验售后商品
     * @status recieved verify
     */
    function changeverify($por_id,$return_id){
        $oOperation_log = &$this->app->model('operation_log');
        $item=$this->db->selectrow('select count(*) as count from sdb_ome_return_process_items  where is_check=\'true\' and  por_id='.$por_id);
        $count=$item['count'];
        $sqlstr='select count(*) as count from sdb_ome_return_process_items  where por_id='.$por_id;
        $pro_item = $this->db->selectrow($sqlstr);
        $countstr=$pro_item['count'];
        if($count==$countstr){
            $sdf_pro = array(
                'por_id'=>$por_id,
                'verify'=>'true'
            );
            $this->save($sdf_pro);//修改从表状态
            $memo = '校验成功';
            $oOperation_log->write_log('return@ome',$por_id,$memo);

        }

       $this->changestatus($por_id,$return_id,'verify','7');
    }
}
?>